package com.jiayinlending.jiayindai.activity.loan;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiayinlending.jiayindai.R;
import com.jiayinlending.jiayindai.activity.base.BaseActivity;
import com.jiayinlending.jiayindai.adapter.TitleRecylerAdpater;
import com.jiayinlending.jiayindai.net.bean.Info;
import com.jiayinlending.jiayindai.util.ToastUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jiayinlending.jiayindai.R.id.et_loan_description;
import static com.jiayinlending.jiayindai.R.id.ll_title;
/*
* 借款用途
* */
public class LoanMessageActivity extends BaseActivity {
    @Bind(et_loan_description)
    EditText etLoanDescription;
    @Bind(R.id.tv_word)
    TextView tvWord;
    @Bind(R.id.bt_submit_function)
    Button btSubmitFunction;
    @Bind(R.id.tv_loantitlt)
    EditText tvLoantitlt;
    @Bind(R.id.ll_description)
    LinearLayout llDescription;
    @Bind(R.id.title_recyler)
    RecyclerView titleRecyler;
    @Bind(ll_title)
    LinearLayout llTitle;
    @Bind(R.id.ll_loanmessage)
    LinearLayout llLoanmessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        etLoanDescription.addTextChangedListener(mTextWatcher);
        getToolbarTitle().setText("借款用途");
        getSubTitle().setText("");
        initview();
        tvLoantitlt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {//获取焦点
                    llDescription.setVisibility(View.GONE);
                    llTitle.setVisibility(View.VISIBLE);
                } else {//失去焦点
                    llDescription.setVisibility(View.VISIBLE);
                    llTitle.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_loan_function;
    }

    private void initview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        titleRecyler.setLayoutManager(layoutManager);
        ArrayList<Info> list1 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list1.add(new Info());
        }
        titleRecyler.setAdapter(new TitleRecylerAdpater(this, list1));
    }

    TextWatcher mTextWatcher = new TextWatcher() {
        private final int charMaxNum = 120;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            tvWord.setText(s.toString().length() + "/" + charMaxNum);
        }
    };


    @OnClick({R.id.bt_submit_function, R.id.ll_loanmessage})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_submit_function:
                ToastUtil.show(this, "提交成功");
                break;
            case R.id.ll_loanmessage:
                InputMethodManager imm = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(llLoanmessage.getWindowToken(), 0);
                tvLoantitlt.clearFocus();
                break;
        }
    }
}

